/*
 * Copyright (c) 2010-2020 IISI.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of IISI.
 */
package com.iisigroup.log.analysis.rdbms.dao;
 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 */
public class TableJDBCDao {
    //================================================
    //== [Enumeration types] Block Start
    //== [Enumeration types] Block End 
    //================================================
    //== [static variables] Block Start
    private final static Logger LOGGER = LoggerFactory.getLogger(TableJDBCDao.class);
    //== [static variables] Block Stop 
    //================================================
    //== [instance variables] Block Start
    //== [instance variables] Block Stop 
    //================================================
    //== [static Constructor] Block Start
    //== [static Constructor] Block Stop 
    //================================================
    //== [Constructors] Block Start (含init method)
    //== [Constructors] Block Stop 
    //================================================
    //== [Static Method] Block Start
    //== [Static Method] Block Stop 
    //================================================
    //== [Accessor] Block Start
    //== [Accessor] Block Stop 
    //================================================
    //== [Overrided Method] Block Start (Ex. toString/equals+hashCode)
    //== [Overrided Method] Block Stop 
    //================================================
    //== [Method] Block Start
    //####################################################################
    public static Connection getInstance() {       
//        String url = "jdbc:h2:mem:test;INIT=create schema if not exists test\\;runscript from '~/sql/populate.sql';DB_CLOSE_DELAY=-1";
//        String url = "jdbc:h2:mem:test;INIT=create schema if not exists test\\;runscript from './sql/populate.sql';DB_CLOSE_DELAY=-1";
        String url = "jdbc:h2:./dbdata/testdbh2;INIT=create schema if not exists test\\;runscript from './sql/populate.sql';";
        String username = "sa";
        String password = "";
//        String driver = "org.hsqldb.jdbcDriver";
        String driver = "org.h2.Driver";
        //org.h2.Driver
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            LOGGER.error(e.getMessage() , e) ;
        } catch (ClassNotFoundException e) {
            LOGGER.error(e.getMessage() , e) ;
        }
        return conn;
    }

    
    
   
    public int countTableSize(String tableName){
        int result =0;
        final Connection conn = getInstance();
        Statement stmt;
        try {
            stmt = conn.createStatement();
            String sql = String.format("SELECT  COUNT(*) AS rowcount  FROM %s ",tableName);
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                // Retrieve by column name
                 
                result = rs.getInt("rowcount") ;
 
            }
            rs.close();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
        } finally {
            closeConnection(conn);
        }
        return result;
    } 
    public List<String[]> getPersonIdSiteIdList() {
        final List<String[]> result = new ArrayList<String[]>();
        final Connection conn = getInstance();
        Statement stmt;
        try {
            stmt = conn.createStatement();
            String sql = "SELECT   person_id,site_id FROM rldf004m where personal_mark='0'  ";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                // Retrieve by column name

                final String personId = rs.getString("person_id");
                final String siteId = rs.getString("site_id");

                // Display values
                //System.out.print("person_id: " + personId);
                //System.out.println("site_id: " + siteId);
                result.add(new String[] { personId, siteId });
            }
            rs.close();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
        } finally {
            closeConnection(conn);
        }
        return result;
    }
    protected void closeConnection(final Connection conn ){
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                 LOGGER.error(e.getMessage() , e) ;
            }
        }
    }
    
    //## [Method] sub-block : 
    //####################################################################    
    //== [Method] Block Stop 
    //================================================
    //== [Inner Class] Block Start
    //== [Inner Class] Block Stop 
    //================================================
}
